import db from "../models/index.js";
import {getNow} from "../utils/utils.js";

let user = db.user;
let userDao = {};
//判断是否存在这个用户
userDao.isExisted = (_account) => {
  return user.findOne({
    where: {
      _account
    },
    raw: true,
  });
};
//判断是否存在这个用户邮箱
userDao.isExistedByEmail = (_email) => {
  return user.findOne({
    where: {
      _email
    },
    raw: true,
  });
};
//判断用户是存在且密码正确
userDao.isExistedByPassword = (_account, _password) => {
  return user.findOne({
    where: {
      _account,
      _password,
    },
    raw: true,
  });
};
//获取用户的邮箱
userDao.getUserEmail = (_account) => {
  return user.findOne({
    attributes: ['_email'],
    where: {
      _account,
    },
    raw: true,
  });
};
//注册用户
userDao.addUser = (_account, _password, _email, t) => {
  if (!t) {
    return user.create({_account, _password, _email, _registration_time: getNow()});
  }
  return user.create({_account, _password, _email, _registration_time: getNow()}, {transaction: t});
};
//修改用户密码
userDao.updatePassword = (_account, _password, t) => {
  if (!t) {
    return user.update({_password}, {where: {_account}});
  }
  return user.update({_password}, {where: {_account}, transaction: t});
};
export default userDao;
